/*
 * Copyright 2012 Dominik.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package cz.muni.fi.PA165.musiclibraryweb.genre;

import cz.muni.fi.PA165.musiclibraryweb.HomePage;
import cz.muni.fi.PA165.musiclibrarybackend.dto.GenreDTO;
import cz.muni.fi.PA165.musiclibraryapi.services.GenreService;
import java.io.Serializable;
import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;


@AuthorizeInstantiation({"ADMIN"})
public class AddGenrePage extends HomePage implements Serializable {

    @SpringBean
    private GenreService service; 
    private GenreDTO genre;

    public AddGenrePage(final PageParameters parameters) {
        this(parameters, null);
    }

    //edit
    public AddGenrePage(final PageParameters parameters, GenreDTO genreDTO) {
        super(parameters);
        add(new BookmarkablePageLink("genreView", GenrePage.class));
        add(new BookmarkablePageLink("genreAdd", AddGenrePage.class));

        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        service = context.getBean(GenreService.class);

        if (genreDTO == null) {
            this.genre = new GenreDTO();
        } else {
            this.genre = genreDTO;
        }

        final TextField<String> nameField = new TextField("name", new PropertyModel<String>(genre, "name"));

        Form<?> form = new Form("form") {
            @Override
            protected void onSubmit() {
                if (nameField.getConvertedInput() == null || nameField.getConvertedInput().equals("")) {
                    setResponsePage(new GenrePage(parameters, new Label("error", "Genre name should be filled.")));
                } else {
                    if (genre.getId() != null) {
                        genre.setName(nameField.getValue());
                        try {
                            service.updateGenre(genre);
                            setResponsePage(GenrePage.class);
                        } catch (Exception exception) {
                            setResponsePage(new GenrePage(parameters,
                                    new Label("error", "Database error!")));
                        }
                    } else {
                        try {
                            service.createGenre(genre);
                            setResponsePage(GenrePage.class);
                        } catch (Exception exception) {
                            setResponsePage(new GenrePage(parameters,
                                    new Label("error", "Database error!")));
                        }
                    }
                }
            }
        };
        form.add(nameField);
        add(form);
    }
}
